package server;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PendingGameReqServlet extends HttpServlet {

	private static final long serialVersionUID = -4787028292097808179L;
	public PendingGameReqServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			response.setContentType("application/x-java-serialized-object");
			InputStream in = request.getInputStream();
			ObjectInputStream inputFromApplet = new ObjectInputStream(in);
			java.lang.Object o =  inputFromApplet.readObject();
			String name = o.toString();			
			//return the value to client
			int result = WebGamesManager.getInstance().findGameReq(name);
			OutputStream outstr = response.getOutputStream();
			ObjectOutputStream oos = new  ObjectOutputStream(outstr);
			oos.writeObject(result);
			oos.flush();
			oos.close();

		} catch (Exception e) { 
			e.printStackTrace();
		}		
		
		
	}

	private boolean CheckIfExsists(String newPlayer) {
		// TODO Auto-generated method stub
		return true;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
